package com.iocoder.yudao.module.erp.service.warehouse;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.iocoder.yudao.module.erp.controller.admin.stock.vo.warehouse.ErpWarehousePageVO;
import com.iocoder.yudao.module.erp.controller.admin.stock.vo.warehouse.ErpWarehouseSaveVo;
import com.iocoder.yudao.module.erp.mapper.warehouse.ErpWarehouseMapper;
import com.iocoder.yudao.module.erp.model.warehouse.ErpWarehouseDO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static erp.enums.ErrorCodeConstants.WAREHOUSE_NOT_EXISTS;

@Service
public class ErpWarehouseServiceImpl implements ErpWarehouseService{

    @Resource
    private ErpWarehouseMapper erpWarehouseMapper;

    @Override
    public PageResult<ErpWarehouseDO> getWarehousePage(ErpWarehousePageVO erpWarehousePageVO) {

        return erpWarehouseMapper.selectPage(erpWarehousePageVO);
    }

    @Override
    public Long createWarehouse(ErpWarehouseSaveVo warehouseSaveVo) {

        ErpWarehouseDO warehouseDO= BeanUtils.toBean(warehouseSaveVo,ErpWarehouseDO.class);
        erpWarehouseMapper.insert(warehouseDO);
        return warehouseDO.getId();
    }

    @Override
    public void updateWarehouse(ErpWarehouseSaveVo updateReqVO) {

        ErpWarehouseDO warehouseDO= BeanUtils.toBean(updateReqVO,ErpWarehouseDO.class);
        erpWarehouseMapper.updateById(warehouseDO);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWarehouseDefaultStatus(Long id, Boolean defaultStatus) {

        //1.校验是否存在
        validateWarehouseExists(id);
        // 2 如果开启，则需要关闭所有其它的默认
        if(defaultStatus){
            ErpWarehouseDO warehouseDO=erpWarehouseMapper.selectByDefaultStatus();
            if(warehouseDO!=null){
                erpWarehouseMapper.updateById(new ErpWarehouseDO().setId(warehouseDO.getId()).setDefaultStatus(false));
            }
        }
        erpWarehouseMapper.updateById(new ErpWarehouseDO().setId(id).setDefaultStatus(defaultStatus));
    }


    @Override
    public void deleteWarehouse(Long id) {

        validateWarehouseExists(id);
        erpWarehouseMapper.deleteById(id);
    }

    private void validateWarehouseExists(Long id) {

        if (erpWarehouseMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    @Override
    public ErpWarehouseDO getWarehouse(Long id) {

        return  erpWarehouseMapper.selectById(id);
    }

    @Override
    public List<ErpWarehouseDO> getWarehouseList(Collection<Long> ids) {

        if(CollUtil.isEmpty(ids)){
            return Collections.emptyList();
        }
        return erpWarehouseMapper.selectBatchIds(ids);
    }
}
